package com.shop.user.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.shop.core.HttpMessageResult;
import com.shop.entity.ApUser;
import com.shop.user.common.UserInfoEnum;
import com.shop.user.service.ApUserService;
import com.shop.utils.JwtUtil;

@RestController
@CrossOrigin
@RefreshScope
@RequestMapping("/user")
public class ApUserController  {

	@Autowired
	private ApUserService userService;
	
	@Autowired
	private JwtUtil jwtUtil;
	
	@Value("${server.port}")
    private Integer port;

	@Value("${wechat.appId}")
	private String appId;
	
	@RequestMapping(value = "/get_by_uname/{username}", method = RequestMethod.GET)
    public ApUser getUserByName(@PathVariable String username) {
		System.out.println("负载端口-->" + port);
        return userService.getUserByUsername(username);
    }
	
	/**
	 * 登录
	* @Title: doUserLogin
	* @Description: TODO(这里用一句话描述这个方法的作用)
	* @param @param user
	* @param @return    设定文件
	* @return HttpMessageResult    返回类型
	* @throws
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public HttpMessageResult doUserLogin(@RequestBody ApUser user){
		ApUser uInfo = userService.doUserLogin(user.getUsername(), user.getPassword());
		if(null == uInfo){
			return HttpMessageResult.doHttp(UserInfoEnum.USER_LOGIN_ERROR.getCode(), UserInfoEnum.USER_LOGIN_ERROR.getMessage());
		}
		String token = jwtUtil.createJWT(uInfo.getUserId(), user.getRealName(), "user","");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("token", token);
		map.put("roles", "user");
		return HttpMessageResult.doHttp(UserInfoEnum.USER_LOGIN_SUCCESS.getCode(), UserInfoEnum.USER_LOGIN_SUCCESS.getMessage(), map);
	}

	
}
